package com.framework.yhbapi.controller;

import com.framework.exception.util.RespResult;
import com.framework.service.yhbservice.YhbAddreesService;
import com.framework.yhbEntity.user.YhbUserAddress;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 功能描述: 收货地址控制器
 *
 * @author ck
 * @date 2018/9/18 11:48
 */
@RestController
@Api(value="收货地址模块",tags={"收货地址模块"})
@RequestMapping(value = "yhbAddress")
public class YhbAddressController {

	@Autowired
	private YhbAddreesService yhbAddreesService;

	/**
	 * 获取收货地址列表
	 * @param token
	 * @return
	 */
	@GetMapping("/user/getAddressByToken")
	@ApiOperation("获取商户收货地址列表")
	@ApiImplicitParam(name="token",value="登录产生的token",dataType="String", paramType = "query",required = true)
	public RespResult getAddressByToken(String token){
		List<YhbUserAddress> addressList = yhbAddreesService.getAddressByToken(token);
		return RespResult.ok().put("addressList",addressList);
	}

	/**
	 * 添加收货地址
	 * @param address
	 * @param token
	 * @return
	 */
	@PostMapping("/user/addAddress")
	@ApiOperation("添加收货地址")
	@ApiImplicitParams({
			@ApiImplicitParam(name="address",value="收货地址",dataType="com.framework.yhbEntity.user.YhbUserAddress", paramType = "query",required = true),
			@ApiImplicitParam(name="token",value="登录产生的token",dataType="String", paramType = "query",required = true)
	})
	public  RespResult addAddress(YhbUserAddress address,String token){
		yhbAddreesService.addAddress(address,token);
		return RespResult.ok();
	}


	/**
	 * 添加收货地址 未注册时使用
	 * @param address
	 * @return
	 */
	@PostMapping("/addAddressByPhone")
	@ApiOperation("添加收货地址（未注册时使用）")
	@ApiImplicitParams({
			@ApiImplicitParam(name="address",value="收货地址",dataType="com.framework.yhbEntity.user.YhbUserAddress", paramType = "query",required = true)
	})
	public  RespResult addAddressByPhone(YhbUserAddress address){
		yhbAddreesService.addAddressByPhone(address);
		return RespResult.ok();
	}

	/**
	 * 修改地址
	 * @param address
	 * @param token
	 * @return
	 */
	@PostMapping("/user/updateAddress")
	@ApiOperation("修改地址")
	@ApiImplicitParams({
			@ApiImplicitParam(name="address",value="收货地址",dataType="com.framework.yhbEntity.user.YhbUserAddress", paramType = "query",required = true),
			@ApiImplicitParam(name="token",value="登录产生的token",dataType="String", paramType = "query",required = true)
	})
	public RespResult updateAddress(YhbUserAddress address,String token){
		yhbAddreesService.updateAddress(address,token);
		return RespResult.ok();
	}

	/**
	 * 获取默认收货地址
	 * @param token
	 * @return
	 */
	@PostMapping("/user/getDefaultAddreess")
	@ApiOperation("获取默认地址")
	@ApiImplicitParam(name="token",value="登录产生的token",dataType="String", paramType = "query",required = true)
	public RespResult getDefaultAddreess(String token){
		YhbUserAddress address = yhbAddreesService.getDefaultAddreess(token);
		return RespResult.ok().put("address",address);
	}


	@PostMapping("/user/deleteAddress")
	@ApiOperation("删除地址")
	@ApiImplicitParams({
			@ApiImplicitParam(name="token",value="登录产生的token",dataType="String", paramType = "query",required = true),
			@ApiImplicitParam(name="addressId",value="地址id",dataType="String", paramType = "query",required = true)
	})
	public RespResult deleteAddress(String token,String addressId){
		yhbAddreesService.deleteAddress(token,addressId);
		return RespResult.ok();
	}

	/**
	 * 得到收货地址 未注册时使用
	 * @param phone
	 * @return
	 */
	@PostMapping("/getAddressByPhone")
	@ApiOperation("获取收货地址（未注册时使用）")
	@ApiImplicitParams({
			@ApiImplicitParam(name="phone",value="收货地址",dataType="String", paramType = "query",required = true),
	})
	public  RespResult getAddressByPhone(String phone){
		YhbUserAddress address = yhbAddreesService.queryAddressByPhone(phone);
		return RespResult.ok().put("address",address);
	}

}
